/**
 * 初始化发货人信息详情对话框
 */
var ConsignerInfoDlg = {
    consignerInfoData : {},
    validateFields: {
    	consignerName: {
            validators: {
                notEmpty: {
                    message: '发货人不能为空'
                }
            }
        },
        consignerMobile: {
            validators: {
                notEmpty: {
                    message: '手机号不能为空'
                }
            }
        },
        consignerPlace: {
            validators: {
                notEmpty: {
                    message: '发货人地址不能为空'
                }
            }
        }
    }
};

/**
 * 验证数据是否为空
 */
ConsignerInfoDlg.validate = function () {
    $('#consignerForm').data("bootstrapValidator").resetForm();
    $('#consignerForm').bootstrapValidator('validate');
    return $("#consignerForm").data('bootstrapValidator').isValid();
};

/**
 * 清除数据
 */
ConsignerInfoDlg.clearData = function() {
    this.consignerInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsignerInfoDlg.set = function(key, val) {
    this.consignerInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsignerInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ConsignerInfoDlg.close = function() {
    parent.layer.close(window.parent.Consigner.layerIndex);
}

/**
 * 收集数据
 */
ConsignerInfoDlg.collectData = function() {
    this
    .set('id')
    .set('consignerId')
    .set('consignerName')
    .set('consignerGender')
    .set('consignerCard')
    .set('consignerPlace')
    .set('consignerTell')
    .set('consignerMobile')
    .set('consignerCompanyName')
    .set('consignerCompanyPlace')
    .set('consignerCompanyTell')
    .set('consignerRememberId');
}

/**
 * 提交添加
 */
ConsignerInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consigner/add", function(data){
        Feng.success("添加成功!");
        window.parent.Consigner.table.refresh();
        ConsignerInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consignerInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ConsignerInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consigner/update", function(data){
        Feng.success("修改成功!");
        window.parent.Consigner.table.refresh();
        ConsignerInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consignerInfoData);
    ajax.start();
}

$(function() {
	//初始化性别选项
    $("#consignerGender").val($("#sexValue").val());
	 Feng.initValidator("consignerForm", ConsignerInfoDlg.validateFields);
});
